package com.mycode.config;


import java.util.List;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

    // 在拦截器调用方法之前,拦截SOAP消息.
    public AuthInterceptor() {
        super(Phase.PRE_INVOKE);
    }

    @Override
    /**
     * 拦截器处理方法
     */
    public void handleMessage(SoapMessage message) throws Fault {
        System.out.println("---- 进入拦截器 ----");

        // 获取SOAP携带的所有Header
        List<Header> headers = message.getHeaders();
        if (null == headers || headers.size() < 1)
            throw new Fault(new IllegalArgumentException("拦截器实施拦截 , 没有Header"));

        // 获取Header携带的用户名和密码
        Header firstHeader = headers.get(0);
        Element element = (Element) firstHeader.getObject();

        NodeList userNameElement = element.getElementsByTagName("userName");
        NodeList passWordElement = element.getElementsByTagName("passWord");

        if (userNameElement.getLength() != 1)
            throw new Fault(new IllegalArgumentException("用户名不能为空.."));
        if (passWordElement.getLength() != 1)
            throw new Fault(new IllegalArgumentException("密码不能为空.."));

        // 获取元素中的文本内容
        String username = userNameElement.item(0).getTextContent();
        String password = passWordElement.item(0).getTextContent();

        // 实际项目中,是从数据库中取数据做校验
        if (!username.equals("saleservice.axentbath@2018") || !password.equals("saleservice@2018")) {
            throw new Fault(new IllegalArgumentException("用户名或密码不正确.."));
        } else {
            System.out.println("验证成功!!");
        }

    }

}